extends Node

var _pause = false
var storyScript = null
var fileName = ''
var currIndex = -1
var _currTick = 0
var _maxTick = -1
var tagIndexs = {}

func loadConfig(_fileName : String):
	storyScript = Model.loadFileAsLine(_fileName)
	fileName = _fileName
	currIndex = 0
	_pause = false
	indexTags()
	return storyScript != null

func pause(ms : int):
	_pause = true
	_maxTick = ms
	_currTick = 0
	pass
	
func resume():
	_pause = false
	_maxTick = -1
	_currTick = 0
	pass
	
func isPause():
	return _pause

func update(dt : float):
	if _pause && _maxTick != -1:
		_currTick += dt * 1000.0
		#print(_currTick, _maxTick)
		if _currTick >= _maxTick:
			resume()
	pass
	
func isEnd():
	if storyScript == null:
		return true
	return currIndex >= storyScript.size()

func nextStep():
	currIndex += 1
	pass
	
func backStep():
	currIndex -= 1
	pass
	
func currCode():
	if storyScript == null:
		return ''
	return storyScript[currIndex]
	
func currCodeIsNotTag():
	var code = currCode()
	return code.find("*") != 0
	
func seekTag(tag : String):
	if tagIndexs.has(tag) == false:
		return
	currIndex = tagIndexs[tag]
	pass
	
func indexTags():
	if storyScript == null:
		return
	for i in range(storyScript.size()):
		var code = storyScript[i]
		if code.find("*") == 0:
			tagIndexs[code] = i
			pass
	#print(tagIndexs)
	pass
	
func index():
	return currIndex
	
func name():
	return fileName
	
func getSaveData():
	return {
		"fileName": fileName,
		"index": currIndex
	}
	
func setSaveData(data : Dictionary):
	loadConfig(data.fileName)
	currIndex = data.index
	print(data)
